package com.cooper.tld;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Base Taglib for user-defined tag class
 * @author Coraldane coraldane@gmail.com
 * @Date Jul 10, 2010 18:07:30
 * @version 1.1
 */
public class CoralBaseTag extends TagSupport {
	
	private static final long serialVersionUID = -1672737463837284641L;
	
	protected Logger logger = LoggerFactory.getLogger(CoralBaseTag.class);
	
	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected StringBuffer buffer = new StringBuffer();

	public CoralBaseTag() {
		
	}

	public HttpServletRequest getRequest() {
		return this.request;
	}
	
	public HttpServletResponse getResponse() {
		return this.response;
	}
	
	protected JspWriter getOut() throws IOException {
		return pageContext.getOut();
	}

	protected void write(String html) {
		try {
			logger.debug(html);
			this.getOut().write(html);
		} catch (IOException e) {
			logger.error("Error:", e);
		}
	}

	protected void write() {
		this.write(this.buffer.toString());
	}

	protected void initRequest() {
		this.request = (HttpServletRequest) pageContext.getRequest();
		this.response = (HttpServletResponse) pageContext.getResponse();
	}
	
	public int doStartTag() throws JspException {
		this.initRequest();
		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() throws JspException {
		super.release();
		return EVAL_PAGE;
	}
	
}